#!/usr/bin/env bash
# ... if sh is not enough

#
# 复制activity
#

# "$@" = "$1" "$2" "$3" ... "$n"
# "$*" = "$1y$2y$3y...$n", where y is the value of IFS variable i.e. 
# "$*" is one long string and $IFS act as an separator or token delimiters.
# Use Uppercase Names for ENVIRONMENT Variables and Lowercase for Custom Variables

# IFS='\n\t' # Default IFS for “zsh” is: $' \n\t\0'.

set -euo pipefail
# set -o xtrace

declare -r script_path="${BASH_SOURCE[0]}"
declare -r script_dir="${script_path%/*}"
declare -r script_file="${script_path##*/}"

# path="dir/basename.ext"
# dir="${path%/*}"
# filename="${path##*/}" # basename.ext
# basename="${path%\.*}"
# ext=${path##*.}


info() { printf "\\e[1m\\e[38;5;14m✔ %s\\e[0m\\n" "$@"; }
warn() { printf "\\e[1m\\e[38;5;148m➜️ %s\\e[0m\\n" "$@"; }
error(){ printf "\\e[1m\\e[38;5;196m✖ %s\\e[0m\\n" "$@"; }

die() {
    local last_error=$?
    if [[ $1 =~ ^[0-9]+$ ]]; then
        last_error=$1
        shift
    fi
    error "${script_file}:${BASH_LINENO[0]} ${FUNCNAME[1]}(${last_error})>$*" >&2
    exit "$last_error"
}

#declare -i # for integer
#declare -a # for array
#declare -r # readonly

if [[ $# -lt 1   ]]; then
    echo "Usage:${script_file} module-name manager-only"
    exit 1
fi

main(){
    current_dir="${script_dir}"
    

    xiaoxie_danshu="$1"
    daxie_danshu="$(tr '[:lower:]' '[:upper:]' <<< "${xiaoxie_danshu:0:1}")${xiaoxie_danshu:1}"

    xiaoxie="$xiaoxie_danshu""s"
    daxie="${daxie_danshu}""s"


    echo "$xiaoxie $daxie $xiaoxie_danshu $daxie_danshu"
        
    manager_only=${2:-true}
    # if [[ $manager_only == "true" ]]; then
    #    echo "manager_only"
    # fi

    # exit ;

    VIEW_OR_RUN=""

    class_path="$current_dir/app/src/main/java/all/com/jinyongqunxiax/modules/MenpaisActivity.java"
    layout_path="$current_dir/app/src/main/res/layout/menpais_activity.xml"
    item_layout_path="$current_dir/app/src/main/res/layout/menpais_menpai_item.xml"
    manager_path="$current_dir/app/src/main/java/all/com/jinyongqunxiax/model/mod/MenpaiManager.java"
    
    new_class_path=$(echo "$class_path" | sed "s#Menpais#$daxie#g")
    new_layout_path=$(echo "$layout_path" | sed "s#menpais#$xiaoxie#g")
    new_item_layout_path=$(echo "$item_layout_path" | sed "s#menpais#$xiaoxie#g" | sed "s#menpai#$xiaoxie_danshu#g")
    new_manager_path=$(echo "$manager_path" | sed "s#Menpai#$daxie_danshu#g" | sed "s#menpai#$xiaoxie_danshu#g")


    if [[ ! $manager_only == "true" ]]; then
        $VIEW_OR_RUN cp -f "$class_path" "$new_class_path"
        $VIEW_OR_RUN cp -f "$layout_path" "$new_layout_path"
        $VIEW_OR_RUN cp -f "$item_layout_path" "$new_item_layout_path"

        $VIEW_OR_RUN sed -i '' -e "s#Menpais#$daxie#g" -e "s#menpais#$xiaoxie#g" -e "s#Menpai#$daxie_danshu#g" -e "s#menpai#$xiaoxie_danshu#g" "$new_class_path"
        $VIEW_OR_RUN sed -i '' -e "s#Menpais#$daxie#g" -e "s#menpais#$xiaoxie#g" -e "s#Menpai#$daxie_danshu#g" -e "s#menpai#$xiaoxie_danshu#g" "$new_layout_path"
        $VIEW_OR_RUN sed -i '' -e "s#Menpais#$daxie#g" -e "s#menpais#$xiaoxie#g" -e "s#Menpai#$daxie_danshu#g" -e "s#menpai#$xiaoxie_danshu#g" "$new_item_layout_path"
    fi
    
    $VIEW_OR_RUN cp -f "$manager_path" "$new_manager_path"
    $VIEW_OR_RUN sed -i '' -e "s#Menpais#$daxie#g" -e "s#menpais#$xiaoxie#g" -e "s#Menpai#$daxie_danshu#g" -e "s#menpai#$xiaoxie_danshu#g" "$new_manager_path"

}

main "$@"